package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.pojo.dto.ClearMessageBody;
import com.ruoyi.pojo.dto.QuestionDto;
import com.ruoyi.pojo.dto.SearchQuestionDto;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.Question;
import com.ruoyi.service.IQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户反馈Controller
 *
 * @author qyb
 * @date 2023-12-26
 */
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RequestMapping("/question")
public class QuestionController extends BaseController
{

    private final IQuestionService questionService;


    /**
     * 反馈列表
     * @param questionDto
     * @return
     */
    @PostMapping("list")
    public TableDataInfo list(@RequestBody SearchQuestionDto questionDto){
        return questionService.getList(questionDto);

    }
    /**
     * 添加用户反馈
     * @param questionDto
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addQuestion(@RequestBody QuestionDto questionDto ){
        if (StringUtils.isBlank(questionDto.getContent())||StringUtils.isBlank(questionDto.getDescription())||questionDto.getUserId()==null){
            return AjaxResult.error("请填写详细信息");
        }else {
            return questionService.addQuestion(questionDto);

        }
    }

    /**
     * 删除反馈
     * @param uuid
     * @return
     */
    @DeleteMapping("/delete/{uuid}")
    public AjaxResult deleteQuestion(@PathVariable Long uuid){
        return questionService.deleteQuestion(uuid);
    }

    /**
     * 已读消息
     * @param
     * @param body (1,用户已读，2管理员已读）
     * @return
     */
    @PostMapping("clear")
    public AjaxResult clear(@RequestBody ClearMessageBody body) {
        if (body.getUuid()==null){
            return AjaxResult.error(516,"操作失败失败");
        }
        return questionService.clearMessage(body.getUuid());
    }

    /**
     * 管理员回复反馈
     * @param questionDto
     * @return
     */
    @PostMapping("backMessage")
    public AjaxResult backMessage(@RequestBody QuestionDto questionDto){
        if (questionDto.getUserId()==null|| questionDto.getUuid()==null||StringUtils.isBlank(questionDto.getBackContent())){
            return AjaxResult.error("回复失败");
        }
        return questionService.backMessage(questionDto);
    }

    /**
     * 反馈详情
     * @param uuid 反馈id
     * @param type 1，普通用户获取2，管理员获取详情
     * @return
     */
    @GetMapping("info")
    public AjaxResult getQuestionInfo(Long uuid,Integer type,Long userId){
        if (uuid==null){
            return AjaxResult.error("516","操作失败失败");
        }
        return questionService.getQuestionInfo(uuid,type,userId);
    }

    /**
     * 清除所有未读消息
     * @param userId
     * @return
     */
    @DeleteMapping("clearAll/{userId}")
    public AjaxResult clearAll(@PathVariable Long  userId) {
        if (userId==null){
            return AjaxResult.error(516,"操作失败");
        }
        return questionService.clearAllMessage(userId);
    }

    /**
     * 导出用户反馈列表
     */
    @PreAuthorize("@ss.hasPermi('library:question:export')")
    @Log(title = "用户反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "用户反馈数据");
    }


}
